package com.cjz.rxjavalearn;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.TextView;

import com.cjz.rxjavalearn.MovieListFragment.OnListFragmentInteractionListener;
import com.cjz.rxjavalearn.pojo.Top250MovieData;

import java.util.List;

/**
 * {@link RecyclerView.Adapter} that can display a {@link Top250MovieData.SubjectsBean} and makes a call to the
 * specified {@link OnListFragmentInteractionListener}.
 */
public class SubjectsBeanRecyclerViewAdapter extends RecyclerView.Adapter<SubjectsBeanRecyclerViewAdapter.ViewHolder> {

    private final List<Top250MovieData.SubjectsBean> mValues;
    private final OnListFragmentInteractionListener mListener;

    public SubjectsBeanRecyclerViewAdapter(List<Top250MovieData.SubjectsBean> items, OnListFragmentInteractionListener listener) {
        mValues = items;
        mListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_subjectsbean, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Top250MovieData.SubjectsBean item = mValues.get(position);
        if (item != null) {
            holder.mItem = item;
            holder.mContentView.setText(item.getTitle());
            holder.rbRating.setRating((float) (item.getRating().getAverage() / 2.0));
            holder.mView.setOnClickListener(v -> {
                if (null != mListener) {
                    mListener.onListFragmentInteraction(holder.mItem);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        final View mView;
        final TextView mContentView;
        Top250MovieData.SubjectsBean mItem;
        RatingBar rbRating;


        ViewHolder(View view) {
            super(view);
            mView = view;
            mContentView = (TextView) mView.findViewById(R.id.content);
            rbRating = (RatingBar) mView.findViewById(R.id.rb_rating);
        }

    }
}
